package com.android.locationapi.maps;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class displayfriends extends ListActivity
{
	//ProgressDialog myProgressDialog = null;
	public static String[] FRIENDS = null;
    
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
	      final Context ctx = this.getApplicationContext();
		  Toast.makeText(ctx,"Error!!! :: Failed to add friends", 6000).show();
		  
		  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 	      nameValuePairs.add(new BasicNameValuePair("username",creden.usernme));
 	      
          InputStream is = null;
  	      String result = "";
  	      try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(serveripaddress.servipaddr+"fetch_friends.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.v("Ramya", "AddFriends I am done 1!!!");
            HttpResponse response = httpclient.execute(httppost);
            Log.v("Ramya", "AddFriends I am done 2!!!");
            HttpEntity entity = response.getEntity();
            Log.v("Ramya", "AddFriends here now");
            is = entity.getContent();
            Log.v("Ramya: Returned from the server 0 : ", is.toString());

  	      }catch(Exception e){
  	    		Log.v("log_tag", "Error in http connection "+e.toString());
  	      }
  	      
  	      try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line);
            }
            is.close();
            result=sb.toString();
            FRIENDS= result.split(",");
            
            Log.v("Ramya: Returned from the server : ", result);
    	 
  	      }
  	      catch(Exception e){
  	    	  Log.v("log_tag", "Error converting result "+e.toString());
  	      }  
          
		  setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, FRIENDS));
		  ListView lv = getListView();
		  lv.setTextFilterEnabled(true);

		  lv.setOnItemClickListener(new OnItemClickListener() {
		  public void onItemClick(AdapterView<?> parent, View view,
		  int position, long id) {
		  
		  Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		  Toast.LENGTH_SHORT).show();
		    }
		  });
  	      }
  	      
	}
